package com.kuqi.crs.settings.service.impl;

import com.kuqi.crs.exception.LoginException;
import com.kuqi.crs.settings.dao.UserDao;
import com.kuqi.crs.settings.domain.User;
import com.kuqi.crs.settings.service.UserService;
import com.kuqi.crs.util.DateTimeUtil;
import com.kuqi.crs.util.SqlSessionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:kuqi
 * @createTime:2021/9/23 20:00
 * @description:
 */
public class UserServiceImpl implements UserService {
    //动态获取UserDao层的类的实例
    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    public User checkLogin(String loginAct, String loginPwd, String ip) throws LoginException {
        Map<String,String> map = new HashMap<String, String>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        User user = userDao.checkLogin(map);

        //如果账号为null
        if (user == null){
            throw new LoginException("账号密码错误");
        }

        // 验证失效时间
        String currentTime = DateTimeUtil.getSysTime();
        String expireTime = user.getExpireTime();
        if (expireTime.compareTo(currentTime)<0){
            throw new LoginException("账号已失效");
        }

        //验证锁定状态
        String lockState = user.getLockState();
        if ("0".equals(lockState)) {
            throw new LoginException("账号已锁定");
        }
        
        //验证浏览器端的ip地址是否有效
        String allowIps = user.getAllowIps();
        if (allowIps.contains(ip)){
            throw new LoginException("ip地址无效");
        }

        return user;
    }

    public List<User> getUserList() {
        List<User> userList = userDao.getUserList();
        return userList;
    }
}
